package com.ra.config.security;

import com.alibaba.fastjson.JSON;
import com.ra.constant.AuthConstant;
import com.ra.pojo.RespBean;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.ra.enums.StatusCodeEnum.NO_LOGIN;

/**
 * Author : 今晚几点睡_Ada
 * Date: 2023/3/9
 * Time: 13:46
 * 注:  当未登录或者token失效时访问接口时，自定义的返回结果
 */
@Component
public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        httpServletResponse.setContentType(AuthConstant.APPLICATION_JSON);
        httpServletResponse.getWriter().write(JSON.toJSONString(RespBean.error(NO_LOGIN)));
    }
}
